package com.tree.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tree.backend.common.Result;
import com.tree.backend.entity.dto.PowerZoneDTO;
import com.tree.backend.entity.pojo.PowerZone;
import com.tree.backend.entity.vo.PowerZoneVO;
import com.tree.backend.service.IPowerZoneService;
import com.tree.backend.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/powerZone")
public class PowerZoneController {
    private final IPowerZoneService powerZoneService;

    @GetMapping("/list")
    public Result<Page<PowerZoneVO>> getList(PowerZoneDTO powerZoneDTO) {
        return powerZoneService.getList(powerZoneDTO);
    }


    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(powerZoneService.removeById(id));
    }

    @PutMapping
    public Result<Boolean> update(@RequestBody PowerZoneDTO powerZoneDTO) {
        // 直接转换
        PowerZone powerZone = new PowerZone();
        BeanUtils.copyProperties(powerZoneDTO, powerZone);
        return Result.success(powerZoneService.updateById(powerZone));
    }

    @PostMapping
    public Result<Boolean> add(@RequestBody PowerZoneDTO powerZoneDTO) {
        PowerZone powerZone = new PowerZone();
        BeanUtils.copyProperties(powerZoneDTO, powerZone);
        powerZone.setCompanyId(UserContext.getCompanyId());
        return Result.success(powerZoneService.save(powerZone));
    }
    
    /**
     * 获取区域ID和名称的映射
     * @return 区域ID和名称的Map
     */
    @GetMapping("/idNameMap")
    public Result<Map<Integer, String>> getIdNameMap() {
        return powerZoneService.getIdNameMap();
    }
}
